﻿using IBLL;
using IDAL;
using MD5Hash;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class EmployeeBLL : IEmployeeBLL
    {
        public EmployeeBLL(IMemberDAL<EmployeeModel>dal)
        {
            Dal = dal;
        }

        public IMemberDAL<EmployeeModel> Dal { get; }

        public async Task<int> AddEmployee(EmployeeModel employee)
        {
            employee.Passward=employee.Passward.GetMD5();
            var s = Dal.GetMembersAsync().Any(a => a.EmployeeName == employee.EmployeeName);
            if (!s)
            {
                
                return await Dal.AddMemberAsync(employee);
            }
            else 
            {
                return -1;
            }
            
        }

        public EmployeeModel Login(string name, string pwd)
        {
            var sql=Dal.GetMembersAsync().FirstOrDefault(a=>a.EmployeeName==name);
            if (sql == null)
            {
                sql = new EmployeeModel { Eid = -1 };
            }
            else 
            {
                if (sql.Passward != pwd.GetMD5()) 
                {
                    sql=new EmployeeModel { Eid = -2 };
                }
            }
            return sql;
        }
    }
}
